<?php

function list_jadwal_kerja(){
	my_set_code_js('

	function confirmDelete(id){
		var t = confirm(\'Yakin akan menghapus data ?\');
		if(t){
			location.href=\'index.php?com=jadwal_kerja&task=delete&id=\'+id;
		}
		return false;
	}

	');
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Tipe Waktu Kerja'=>array('style'=>'text-align:center;border-bottom:2px solid;width:25%'),    
		'Kelompok'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),    
		'Hari ke'=>array('style'=>'text-align:center;border-bottom:2px solid;width:20%'),    
		'Jam Masuk'=>array('style'=>'text-align:center;border-bottom:2px solid;width:15%'),    
		'Jam Pulang'=>array('style'=>'text-align:center;border-bottom:2px solid;width:15%'),    
		' '=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'), 
	);
	
	$query = " SELECT *,b.label as label_jenis FROM a_jadwal_kerja a 
		INNER JOIN a_waktu_kerja b ON a.waktu_kerja_id =b.waktu_kerja_id
		INNER JOIN  hari c ON c.hari_id = a.hari_id	
		order by a.waktu_kerja_id, a.kelompok, a.hari_id";
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =35;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com=jadwal_kerja&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
			$kgPagerOBJ->pager_set(
				$pager_url, 
				$total_records, 
				$scroll_page, 
				$per_page, 
				$current_page, 
				$inactive_page_tag, 
				$previous_page_text, 
				$next_page_text, 
				$first_page_text, 
				$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 35;  
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
						'href'=>'index.php?com=jadwal_kerja&task=edit&id='.$ey['jadwal_kerja_id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
						'href'=>'javascript:; ',
						'onclick'=>'javascript:confirmDelete('.$ey['jadwal_kerja_id'].');',
						'title'=>'Delete'
				);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );

		$row[] = array(
			'#'=>position_text_align( $i, 'center'),
			'tipe'=>position_text_align($ey['label_jenis'] , 'center'),
			'kelompok'=>position_text_align($ey['kelompok'] , 'center'),
			'kel'=>position_text_align($ey['label'] , 'center'),
			'mas'=>position_text_align(date('H:i',strtotime($ey['jam_masuk'])), 'center'),
			'pul'=>position_text_align(date('H:i',strtotime($ey['jam_pulang'])) , 'center'),
			' '=>position_text_align($edit_button.' '.$delete_button,'right'),
		);
	}
	$datas = table_rows($row);
	$paging = $kgPagerOBJ ->showPaging();
	return table_builder($header , $datas ,  5 , false, $paging );		
}

function form_jadwal_kerja_create($id){
	$view = form_header( "..." , "..."  );
	$fields=my_get_data_by_id('a_jadwal_kerja','jadwal_kerja_id',$id) ;

	if($id == 0){	
		 $tipes = array();
		 $query_tipe = "SELECT * FROM a_waktu_kerja ORDER BY label";
		 $re = my_query($query_tipe);
		 while($row = my_fetch_array($re) ){
			$tipes[$row['waktu_kerja_id']] =$row['label'];
		 }
		 $tipe = array(
			'name'=>'waktu_kerja_id',
			'id'=>'waktu_kerja_id',
			'value'=>isset($_POST['waktu_kerja_id']) ? $_POST['waktu_kerja_id'] : $fields['waktu_kerja_id'] 
			);
		$form_field_tipe = form_dropdown($tipe , $tipes);
		$view .= form_field_display( $form_field_tipe   , "Tipe Waktu Kerja");
	}else{
		$tipes = my_get_data_by_id('a_waktu_kerja','waktu_kerja_id',$fields['waktu_kerja_id']) ;
		$view .= form_field_display( $tipes['label']   , "Tipe Waktu Kerja" );
	}
	if($id == 0){
		$kelompok = array(
			'name'=>'kelompok',
			'value'=>( isset($_POST['kelompok']) ? $_POST['kelompok'] : $fields['kelompok'] ),
			'id'=>'kelompok',
			'type'=>'text',
			'size'=>'1'
		);
		$form_field_kelompok = form_dynamic($kelompok);
		$view .= form_field_display( $form_field_kelompok   , "Kelompok"   );
	}else{
		$view .= form_field_display( $fields['kelompok']   , "Kelompok" );
	}
	if($id == 0){
		$query ="SELECT * FROM hari ORDER BY hari_id ASC";
		$re = my_query($query);
		$haris = array();
		while($row = my_fetch_array($re ) ){	
			$haris[$row['hari_id']] =$row['label'];
		 }
		 $hari = array(
			'name'=>'hari_id',
			'id'=>'hari_id',
			'value'=>isset($_POST['hari_id']) ? $_POST['hari_id'] : $fields['hari_id'] 
			);
		$form_field_hari = form_dropdown($hari , $haris);
		$view .= form_field_display( $form_field_hari   , "Hari");
	}else{
		$tipes = my_get_data_by_id('hari','hari_id',$fields['hari_id']) ;
		$view .= form_field_display( $tipes['label']   , "Hari");
	}
	
	$jam_masuk = array(
		'name'=>'jam_masuk',
		'id'=>'jam_masuk',
		'value'=>$fields['jam_masuk'],
	);
	$form_jam_masuk = form_time($jam_masuk); 
	$view .= form_field_display( $form_jam_masuk   , "Jam Masuk"   );	

	$jam_pulang = array(
		'name'=>'jam_pulang',
		'id'=>'jam_pulang',
		'value'=>$fields['jam_pulang'],
	);
	$form_jam_pulang = form_time($jam_pulang); 
	$view .= form_field_display( $form_jam_pulang   , "Jam Pulang"   );
	
		$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	
	return $view;
}

function form_jadwal_kerja_submit($id){
	$jam_masuk = set_post_time_data('jam_masuk');
	$jam_pulang = set_post_time_data('jam_pulang');

	if($id > 0){
		$datas = array(  
			'jam_masuk'=>my_type_data_str( $jam_masuk ),
			'jam_pulang'=>my_type_data_str( $jam_pulang ), 
		);
		return my_update_record('a_jadwal_kerja' ,'jadwal_kerja_id' , $id , $datas);
	}	
	$datas = array(  
		'waktu_kerja_id'=>my_type_data_int( $_POST['waktu_kerja_id'] ),
		'kelompok'=>my_type_data_str( $_POST['kelompok'] ),
		'hari_id'=>my_type_data_int( $_POST['hari_id'] ),
		'jam_masuk'=>my_type_data_str( $jam_masuk ),
		'jam_pulang'=>my_type_data_str( $jam_pulang ), 
	);
	return my_insert_record('a_jadwal_kerja' , $datas);
}

function form_jadwal_kerja_validate($id){
	$errsubmit = false;
	$err = array();

	
	if($id==0){	
		$waktu_kerja_id =  trim($_POST["waktu_kerja_id"]); 
		if( $waktu_kerja_id == '0'){
			$errsubmit = true;
			$err[] ="Tipe waktu kerja belum diisi";
		}

		$kelompok =  trim($_POST["kelompok"]); 
		if( $kelompok == ''){
			$errsubmit = true;
			$err[] ="Kelompok belum diisi";
		}	

		$hari_id =  trim($_POST["hari_id"]); 
		if( $hari_id == '0'){
			$errsubmit = true;
			$err[] ="Hari belum diisi";
		}	
		
		$query="SELECT jadwal_kerja_id FROM a_jadwal_kerja 
			WHERE waktu_kerja_id=".$waktu_kerja_id.
			" AND kelompok='".$kelompok."'".
			" AND hari_id=".$hari_id.
			" AND jadwal_kerja_id<>".$id; 
		if (my_num_rows( my_query($query)) >0){
			$errsubmit = true;
			$err[] = "Tipe waktu kerja, kelompok dan hari sudah digunakan";
		}
	}
		
	if( $errsubmit){
		return $err;
	}
	
	return $errsubmit;
}

function remove_jadwal_kerja($id){
	$query= "DELETE FROM a_jadwal_kerja WHERE jadwal_kerja_id  = {$id}";
	return my_query($query);
}  
 